package network;

import gui.MainWindow;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import tool.Message;

public class Connector {

	private String server_ip;
	private int server_port;
	private boolean is_connected;
	private Socket socket;
	private ThreadEcouteServeur threadEcouteServeur;
	
	private MainWindow window;
	
	public Connector() {
		
		this.server_ip = "127.0.0.1";
		this.server_port = 2013;
		is_connected = false;
		threadEcouteServeur = null;
		
		window = null;
	}
	
	public void setIpAddress(String string_ip, String string_port) {
		
		if (!is_connected) {
			
			try {
				if (string_port != null && !"".equals(string_port)) {
					int port = Integer.parseInt(string_port.trim());
					
					if (port > 0 && port < 60000) {
						server_port = port;
					}
				}
				
				if (string_ip != null && !"".equals(string_ip)) {
					InetAddress.getByName(string_ip.trim());
					// Si on arrive ici, alors la string_ip est correcte;
					server_ip = string_ip.trim();
				}
			} catch (UnknownHostException e) {
				System.out.println(e.getMessage());
			}
		}
	}
	
	public void setWindow(MainWindow window) {
		
		if (window != null) {
			this.window = window;
		}
	}
	
	public boolean connect() {
		
		if (!is_connected) {
			
			try {
				socket = new Socket(server_ip, server_port);
				
				threadEcouteServeur = new ThreadEcouteServeur(socket, window, this);
				threadEcouteServeur.start();
				
				is_connected = true;
				System.out.println("Connexion établie avec le serveur\n");
				
				return true;
			} catch (UnknownHostException e) {
				System.out.println(e.getMessage());
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
			
			return false;
		}
		else {
			
			return true;
		}
		
		
	}
	
	public void disconnect() {
		
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		is_connected = false;
	}
	
	
	
	/************************* FONCTIONS D'ENVOI DE COMMANDES *************************/
	
	
	public void pseudoWanted(String pseudo) {
		
		if (is_connected && pseudo != null && !"".equals(pseudo)) {
			Message.writeMessage(socket, Message.connect(pseudo));
		}
	}
	
	public void registerAccount(String pseudo, String password) {
		
		if (is_connected && pseudo != null && !"".equals(pseudo)) {
			Message.writeMessage(socket, Message.register(pseudo, password));
		}
	}
	
	public void accountWanted(String pseudo, String password) {
		
		if (is_connected && pseudo != null && !"".equals(pseudo) && password != null && !"".equals(password)) {
			Message.writeMessage(socket, Message.login(pseudo, password));
		}
	}
	
	public void spectatorModeWanted() {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.spectateur());
		}
	}
	
	public void exit() {
		
		String pseudo = window.getCurrentPseudo();
		
		if (is_connected && pseudo != null && !"".equals(pseudo)) {
			Message.writeMessage(socket, Message.exit(pseudo));
		}
	}
	
	public void guessWord(String mot) {
		
		if (is_connected && mot != null && !"".equals(mot)) {
			Message.writeMessage(socket, Message.guess(mot));
		}
	}
	
	public void sendTchatMessage(String message) {
		
		if (is_connected && message != null && !"".equals(message)) {
			Message.writeMessage(socket, Message.talk(message));
		}
	}
	
	public void cheatSpotted(String pseudo) {
		
		if (is_connected && pseudo != null && !"".equals(pseudo)) {
			Message.writeMessage(socket, Message.cheat(pseudo));
		}
	}
	
	public void changeEpaisseur(int epaisseur) {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.set_size(epaisseur));
		}
	}
	
	public void changeCouleur(int red, int green, int blue) {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.set_color(red, green, blue));
		}
	}
	
	public void passerTour() {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.pass());
		}
	}
	
	public void setLine(int x1, int y1, int x2, int y2) {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.set_line(x1, y1, x2, y2));
		}
	}
	
	public void setCourbe(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
		
		if (is_connected) {
			Message.writeMessage(socket, Message.set_courbe(x1, y1, x2, y2, x3, y3, x4, y4));
		}
	}
}
